package com.czy.blog.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czy.blog.entity.Menu;
import com.czy.blog.service.MenuService;
import com.czy.blog.utils.JsonMapper;
import com.czy.blog.vo.MenuVo;

@Controller
@RequestMapping("/menu")
public class MenuController {
	@Autowired
	private MenuService menuService;

	@RequestMapping("/getPage")
	@ResponseBody
	public String getPage(Model model, String id) {
		List<Menu> menuList = menuService.findByParent(menuService.findParentById(id));
		List<MenuVo> vos = menus2Vos(menuList);
		
		String json =  new JsonMapper().toJson(vos);
		model.addAttribute("pid", id);
		return json;
	}

	/**
	 * List<Menu>  --->  List<MenuVo>
	 * @param menuList
	 * @return
	 */
	private List<MenuVo> menus2Vos(List<Menu> menuList)
	{
		List<MenuVo> vos = new ArrayList<MenuVo>();
		
		for(Menu menu : menuList){
			MenuVo vo = new MenuVo();
			BeanUtils.copyProperties(menu, vo);
			vo.setCz(menu.getId());
			
			vos.add(vo);
		}
		return vos;
	}
	
	/** 批量删除菜单*/
	@RequestMapping("/batchDelete")
	@ResponseBody
	public String batchDelete(Model model, String[] ids) {
		menuService.delete(Arrays.asList(ids));
		return "success";
	}

	/**
	 * 保存菜单
	 * @param menu 需要保存的菜单
	 * @param pid 保存菜单父菜单ＩＤ
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(Model model, Menu menu,String pid) {
		menuService.save(menu,pid);
		return "success";
	}
	
	/**
	 * 菜单添加页面，记录其父菜单ＩＤ
	 */
	@RequestMapping("/add/{pid}")
	public String add(Model model,@PathVariable String pid) {
		model.addAttribute("menu", new Menu());
		model.addAttribute("action", "save");
		return "/menu/menu_edit";
	}
	
	/** 菜单编辑页面*/
	@RequestMapping("/edit/{menuId}")
	public String edit(Model model,@PathVariable String menuId) {
		model.addAttribute("menu", menuService.findById(menuId));
		model.addAttribute("action", "update");
		return "/menu/menu_edit";
	}
	
	/** 更新菜单*/
	@RequestMapping("/update")
	@ResponseBody
	public String update(Model model,@ModelAttribute("updateObj") Menu menu) {
		menuService.update(menu);
		return "success";
	}

	/** 进入菜单管理主页面*/
	@RequestMapping("/main")
	public String main() {
		return "/menu/menu_main";
	}

	/**  加载菜单树 */
	@RequestMapping("/tree")
	@ResponseBody
	public String menuTree() {
		return menuService.generalTreeJson();
	}
	
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 表单请求参数bind=true才执行，仅对update()有效（提交请求中不包含password, registerDate, salt, roles属性值，须预绑定到model）
	 */
	@ModelAttribute
	public void prepare(@RequestParam(defaultValue = "false") Boolean bindable, String id, Model model) {
		if (bindable) {
			model.addAttribute("updateObj", menuService.findById(id));
		}
	}
}
